package xianxin;

/**
 * Created by hasee on 2017/9/28.
 */
public class MyArrayList {
    Student[] list = new Student[10];

    public void add(Student student){//尾部插入数据
        for (int i = 0; i < list.length; i++) {//正序遍历数组,直到找到空位
            if(list[i]==null){
                list[i]=student;//插进空位
                return;
            }
        }
    }

    public MyArrayList sortByGrade(){
        MyArrayList myArrayList = new MyArrayList();
        myArrayList.list=list;

        Student max = myArrayList.list[0];
        for (int i = 0; i < myArrayList.list.length; i++) {
            for (int j = i+1; j < myArrayList.list.length; j++) {
                if(myArrayList.list[j]!=null&&myArrayList.list[j].getGrade()>myArrayList.list[i].getGrade()){
                    Student temp = myArrayList.list[i];
                    myArrayList.list[i] = myArrayList.list[j];
                    myArrayList.list[j]=temp;
                }
            }
        }
        return myArrayList;
    }

    public void printAll(){
        for (int i = 0; i < list.length ; i++) {
            System.out.println(list[i]);
        }
    }
}
